mod BTree;

use crate::{data::log_record::LogRecordPos, options::IndexType};
use crate::options::IteratorOptions;

pub trait Indexer: Sync + Send {
    fn put(&self, key: Vec<u8>, pos: LogRecordPos) -> bool;
    fn get(&self, key: Vec<u8>) -> Option<LogRecordPos>;
    fn delete(&self, key: Vec<u8>) -> bool;

    // 返回索引迭代器
    fn iterator(&self,options:IteratorOptions) -> Box<dyn IndexIterator>;
}


pub fn new_indexer(index_type:IndexType) -> impl Indexer {
    match index_type {
        IndexType::BTree=>BTree::BTree::new(),
        IndexType::SkipList=>todo!(),
        _ =>panic!("unkown index type")
    }

}

// 抽象迭代器接口
pub trait IndexIterator: Send + Sync {
    // 回到迭代器起点，即第一个数据
    fn rewind(&mut self);

    // 根据传入的key查找第一个大于（或者小于）等于的目标Key,根据这个key开始遍历
    fn seek(&mut self, key: Vec<u8>);

    // Next跳转到下一个key，返回None则说明迭代完成。
    fn next(&mut self) -> Option<(&Vec<u8>, &LogRecordPos)>;
}